package ru.papergames.model.binsocket
{
	import ru.papergames.model.binsocket.data.ExtByteArray;
	import ru.papergames.model.binsocket.packets.SocketResponse;
	import ru.papergames.model.binsocket.packets.response.*;
	
	public final class MessagingPacketManager
	{			
		
		public static const RESPONSE_MAP:Object = {
							id_1: 	AuthorizationPassedResponse,						//0x01
							id_2: 	ActionFailedResponse,								//0x02
							id_3: 	AuthTimeoutResponse,								//0x03
							id_4: 	MessageFromWebServerResponse,						//0x04
							id_5: 	AuthFailedUserAlreadyOnlineResponse,				//0x05
							id_6: 	SystemMessageResponse,								//0x06
							
							id_17: 	BattleMapInfoResponse,								//0x11
							id_18: 	BattleMemberInfoAboutHimselfResponse,				//0x12
							id_19: 	BattleMemberListResponse,							//0x13													
							id_20: 	BattlePlayerDisconnectedResponse,					//0x14
							id_21: 	BattleFinishedResponse,								//0x15
							id_22: 	BattlePlayerActionPointsChangedResponse,			//0x16													
							id_23: 	BattlePlayerMovedBallResponse,						//0x17													
							id_24: 	BattleYourTurnEndsResponse,							//0x18													
							id_25: 	BattlePlayerFinishedTurnResponse,					//0x19
							id_26: 	BattleYourTurnResponse,								//0x1A
							id_27: 	BattlePlayerStartedTurnResponse,					//0x1B							
							
							id_29: 	NotifyUserBattleCreateFailedResponse,				//0x1D
							id_30: 	BattleNewBallsThrownResponse,						//0x1E
							id_31: 	BattleRemoveBallsInLinesResponse,					//0x1F							
							id_32: 	BattleIncorrectRouteResponse,						//0x20
							id_33: 	BattleBallNotFoundResponse,							//0x21
							id_34: 	BattleNotYourBallResponse,							//0x22
							
							id_161: PingResponse										//0xA1
		};
		
		public static function getResult(bytes:ExtByteArray):SocketResponse {
			if (!bytes || bytes.length < 1) return null; 
			
			var response:SocketResponse = null;
			
			bytes.position = 0;
			
			var packetId:int = bytes.readUnsignedByte();
			
			try {
			
				var responseClass:Class = RESPONSE_MAP['id_' + packetId] as Class;
				
				if (responseClass) {
					response = new responseClass(bytes);
				}
			
			} catch (e:Error) {
				
			}
			
			return response;
		}

	}
}